import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zhaoxiaowu_app/global/themes.dart';
import 'package:zhaoxiaowu_app/routes/routes.dart';
import 'package:zhaoxiaowu_app/viewmodels/accounting_add_viewmodel.dart';
import 'package:zhaoxiaowu_app/viewmodels/accounting_viewmodel.dart';
import 'package:zhaoxiaowu_app/viewmodels/login_viewmodel.dart';
import 'package:zhaoxiaowu_app/viewmodels/love_image_viewmodel.dart';
import 'package:zhaoxiaowu_app/viewmodels/register_viewmodel.dart';
import 'package:zhaoxiaowu_app/viewmodels/theme_viewmodel.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  SharedPreferences sp = await SharedPreferences.getInstance();
  int colorIndex = sp.getInt("color") ?? 0;
  ThemeViewModel themeViewModel = ThemeViewModel();
  themeViewModel.setColorIndex(colorIndex);

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: LoginViewModel()),
        ChangeNotifierProvider.value(value: themeViewModel),
        ChangeNotifierProvider.value(value: RegisterViewModel()),
        ChangeNotifierProvider.value(value: AccountingViewModel()),
        ChangeNotifierProvider.value(value: AccountingAddViewModel()),
        ChangeNotifierProvider.value(value: LoveImageViewModel()),
      ],
      child: MyApp(),
    ),
  );

  configLoading();
}

void configLoading() {
  EasyLoading.instance
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..progressColor = Colors.white
    // ..backgroundColor = Colors.green
    ..indicatorColor = Colors.white
    ..textColor = Colors.white
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = true;
}

final navigatorKey = GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(360, 690),
      builder: () => MaterialApp(
        navigatorKey: navigatorKey,
        title: 'Flutter Demo',
        theme: ThemeData.light().copyWith(
          primaryColor: themes[Provider.of<ThemeViewModel>(context).colorIndex],
          elevatedButtonTheme: ElevatedButtonThemeData(
            style: ElevatedButton.styleFrom(
              primary: themes[Provider.of<ThemeViewModel>(context).colorIndex],
              textStyle: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),
            ),
          ),
          floatingActionButtonTheme: FloatingActionButtonThemeData(
            backgroundColor: themes[Provider.of<ThemeViewModel>(context).colorIndex],
          ),
        ),
        routes: routes,
        builder: EasyLoading.init(),
      ),
    );
  }
}
